package com.example.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.bean.Goods;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;


@Mapper
@Repository
public interface GoodsMapper extends BaseMapper<Goods> {

    /**
     * 删除给定ID的商品
     *
     * @param gId
     * @return
     */
    @Delete("delete from goods where g_id=#{gId}")
    void deleteById(int gId);

    /**
     * 新增商品
     *
     * @param goods
     */
    @Insert("insert into goods(g_Id,m_Id,g_Name,price,g_Describe,g_img,g_inventory,g_state) " +
            "values(#{gId},#{mId},#{gName},#{price},#{gDescribe},#{gImg},#{gInventory},#{gState})")
    void insertGoods(Goods goods);

    /**
     * 修改商品
     * @param goods
     */
    @Update("update goods set g_name=#{gName},price=#{price},g_describe=#{gDescribe},g_inventory=#{gInventory},g_img=#{gImg} where g_id=#{gId}")
    void updateImgGoods(Goods goods);

    /**
     * 查询商家所属的所有商品
     *
     * @return
     */
    @Select("select * from (select * from goods where m_id=#{mId})table_goods where g_Name Like #{likeName}")
    List<Goods> selectAll(int mId,String likeName);

    /**
     * 用户查询商家所属的所有商品
     *
     * @return
     */
    @Select("select * from goods where g_state='上架' and m_id=#{mId}")
    List<Goods> UserSelectAll(int mId);

    /**
     * 分页查询
     * @return
     */
    @Select("SELECT * FROM (SELECT * FROM (SELECT * FROM goods WHERE m_id = #{mId})table_goods WHERE g_Name LIKE #{likeName})table_good LIMIT #{pageNum},#{pageSize}")
    List<Goods> findPage(int mId, int pageNum, int pageSize,String likeName);

    /**
     * 分页查询所有商品
     * @param gName 前台搜索框的内容
     * @param start 从第几条开始查
     * @param currentCount  分页一页显示的条数
     * @return
     */
    @Select("select * from goods where g_state='上架' and g_name LIKE CONCAT ('%' #{gName} '%') LIMIT #{start},#{currentCount} ")
    List<Goods> selectTotal(String gName, int start, int currentCount);

    /**
     * 后台搜索框查询商品
     *
     * @param likeName
     * @return
     */
    @Select("SELECT * FROM (SELECT * FROM goods WHERE m_id = #{mId})table_goods WHERE g_Name LIKE #{likeName}")
    List<Goods> selectLikeName(int mId,String likeName);

    /**
     * 通过gId查询商品
     *
     * @param gId
     * @return
     */
    @Select("select * from goods where g_Id = #{gId}")
    Goods selectGoodsBygId(int gId);

    /**
     * 商品状态更换
     * @param gId
     * @param gState
     */
    @Update("UPDATE goods SET g_state=#{gState} WHERE g_id =#{gId}")
    void updateGoodsState(int gId,String gState);

    /**
     * 查询在架的商品的图片，查询前5个
     * @return
     */
    @Select("select g_img from goods where g_state='上架' limit 0,5")
    List<String> selectGoodImgs();

    /**
     * 查询在架的商品的数量
     * @return
     */
    @Select("select count(*) from goods where g_state='上架' and g_name LIKE CONCAT ('%' #{gName} '%')")
    int selectTotalGoodsCount(String gName);
}
